import json
from Fingerprint.FeatureExtractor import FeatureExtractor
import math
from os import makedirs
from os.path import exists
import datetime

def get_fp_stats_file(params):
    return '%s%s_%s.json' %(params["data"]["folder"], params["data"]["station"],
        params["data"]["channel"])

def save_fp_stats(params,nfp,ndim):
    fp_stats = {"station": params["data"]["station"],
                "channel": params["data"]["channel"],
                "nfp": nfp,
                "ndim": ndim}
    fname = get_fp_stats_file(params)
    with open(fname,'w') as f:
        json.dump(fp_stats,f)

def get_combined_fp_name(params):
    final_fp_name = '%s.%s.fp' % (params['data']['station'], params['data']['channel'])
    return final_fp_name


def get_combined_ts_name(params):
    final_ts_name = '%s.%s.ts' % (params['data']['station'], params['data']['channel'])
    return final_ts_name


def get_min_fp_length(params):
    return params['fingerprint']['fp_length'] * params['fingerprint']['spec_lag'] + \
           params['fingerprint']['spec_length']


def get_partition_padding(params):
    # add this to end of time series of each partition so we don't have missing fingerprints
    sec_extra = params['fingerprint']['spec_length'] + \
                (params['fingerprint']['fp_length'] - params['fingerprint']['fp_lag']) * \
                params['fingerprint']['spec_lag']
    time_extra = datetime.timedelta(seconds=sec_extra)
    return time_extra


def get_ts_fname(mseed_name):
    idx = mseed_name.rfind('.')
    return 'ts_' + mseed_name[:idx]


def get_fp_fname(mseed_name):
    idx = mseed_name.rfind('.')
    return 'fp_' + mseed_name[:idx]


def init_folder(folders):
    for folder in folders:
        if not exists(folder):
            makedirs(folder)


def get_fp_ts_folders(params):
    fp_folder = params['data']['folder'] + 'fingerprint/'
    ts_folder = params['data']['folder'] + 'timestamps/'
    return fp_folder, ts_folder


def get_mad_fname():
    return '../out/mad/median_mad_74793_1024.txt'



def get_params(path):
    with open(path) as json_data_file:
        params = json.load(json_data_file)
    return params


def get_ntimes(params):
    n = params['fingerprint']['fp_length']
    return 2 ** (int(math.log(n, 2)))


def init_feature_extractor(params):
    feats = FeatureExtractor(sampling_rate=params['fingerprint']['sampling_rate'],
                             window_length=params['fingerprint']['spec_length'],
                             window_lag=params['fingerprint']['spec_lag'],
                             fingerprint_length=params['fingerprint']['fp_length'],
                             fingerprint_lag=params['fingerprint']['fp_lag'],
                             min_freq=params['fingerprint']['min_freq'],
                             max_freq=params['fingerprint']['max_freq'],
                             nfreq=params['fingerprint']['nfreq'],
                             ntimes=get_ntimes(params))  # ntimes 是啥？
    return feats
